package com.example.hanchen.yongkanggy.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hanchen.yongkanggy.R;

import java.util.ArrayList;
import java.util.List;

public class ControlitemAdapter extends RecyclerView.Adapter<ControlitemAdapter.TabViewHolder> {

    private Context mContext;
    private List<String> tabData = new ArrayList<>();
    private OnItemClickListener listener;

    public ControlitemAdapter(Context context, OnItemClickListener listener) {
        this.mContext = context;
        this.listener = listener;
    }


    public void setTabData(List<String> tabData) {
        this.tabData = tabData;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ControlitemAdapter.TabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_control, parent, false);
        return new ControlitemAdapter.TabViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ControlitemAdapter.TabViewHolder holder, int position) {

        if (position == 0) {
            holder.tvTab.setText("正常");
            holder.tvTab.setTextColor(mContext.getResources().getColor(R.color.color_46C068));
            holder.tvTab.setBackgroundResource(R.drawable.bg_line_green_360dp);
        } else if (position == 1) {
            holder.tvTab.setText("已登记");
            holder.tvTab.setTextColor(mContext.getResources().getColor(R.color.color_236EFF));
            holder.tvTab.setBackgroundResource(R.drawable.bg_line_blue_360dp);
        } else {
            holder.tvTab.setText("未登记");
            holder.tvTab.setTextColor(mContext.getResources().getColor(R.color.color_F13939));
            holder.tvTab.setBackgroundResource(R.drawable.bg_line_red_360dp);
        }

    }

    @Override
    public int getItemCount() {
        return tabData.size() == 0 ? 0 : tabData.size();
    }


    class TabViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        LinearLayout ll_home;
        TextView tvTab;


        public TabViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_home = itemView.findViewById(R.id.ll_home);
            tvTab = itemView.findViewById(R.id.tvTab);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                listener.onItemClick(position);
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

    }
}
